$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/buserbrandinfo/queryList',
        datatype: "json",
        colModel: [
			{ label: '品牌名称', name: 'id', index: 'id',align: "center", width: 50, key: true,formatter:function (value,options,rowData) {
					var brandName=rowData["brandName"];
					return "<a onclick='update("+value+")'>"+brandName+"</a>";
				} },
			// { label: '品牌名称', name: 'brandName', index: 'brandName',align: "center", width: 80 , formatter:function(value,options,rowData) {
			// 		var id=rowData["id"];
			// 		return "<a onclick='update("+id+")'>"+value+"</a>";
			// 	}
			// },
			{ label: '会员数', name: 'total',dataType: "int", sortable: true, index: 'total',align: "center", width: 80 ,
				formatter:function(value,options,rowData){
					var userId=rowData["userIds"]+",";
					if( value===null || value=="" ){
						return "<p>" + 0 + "</p>"
					}else{
						//return "<a href=buserinfo.html?brandName=" + encodeURI(userId) + ">" + value + "</a>"
						var id=rowData["id"];
						return "<a href=buserinfo.html?brandId=" + id + ">" + value + "</a>"
					}
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			{ label: '品牌状态', name: 'brandOpenStatus', index: 'brand_open_status',align: "center", width: 80, formatter:function(value,options,rowData) {
					if(value==0){
						return"<p style='color: red'>"+'关闭'+"</p>";
					}else if(value==1){
						return"<p>"+'开放'+"</p>";
					}else{
						return"<p style='color: red'>"+'关闭'+"</p>";
					}
				}
			},
			{ label: '是否处理', name: 'handleStatus', index: 'handle_status',align: "center", width: 80,formatter:function(value,options,rowData) {
					if(value==0){
						return '未处理';
					}else if(value==1){
						return"<p style='color: #9a9a9a'>"+'已处理'+"</p>";
					}else{
						return '未处理';
					}
				}
			},
			{ label: '操作', name: '', index: '',align: "center", width: 80 ,formatter:function(value,options,rowData) {
					var brandName = rowData["brandName"].trim().replace(/\s/g,"");
					var id = rowData["id"];
					var brandOpenStatus=rowData["brandOpenStatus"];
					var handleStatus=rowData["handleStatus"];
					var total=rowData["total"];
					var content="";
					if(handleStatus==1 && total==0){
						content="<button class='btn btn-primary' disabled='disabled' onclick=add(" + brandName + ",'" + id + "')>" + '新增' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary' disabled='disabled' onclick=hb('" + brandName + "','" + id + "')>" + '合并' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary'onclick=vm.del('" + id + "')>" + '删除' + "</button>";
					}else if(total==0&&handleStatus==0) {
						content="<button class='btn btn-primary' onclick=add('" + brandName + "','" + id + "')>" + '新增' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary' disabled='disabled' onclick=hb('" + brandName + "','" + id + "')>" + '合并' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary'onclick=vm.del('" + id + "')>" + '删除' + "</button>";
					}else if(total!=0&&handleStatus==1){
						content="<button class='btn btn-primary'disabled='disabled' onclick=add('" + brandName + "','" + id + "')>" + '新增' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary ' onclick=hb('" + brandName + "','" + id + "')>" + '合并' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary'onclick=vm.del('" + id + "')>" + '删除' + "</button>";
					}else{
						content="<button class='btn btn-primary' onclick=add('" + brandName + "','" + id + "')>" + '新增' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary ' onclick=hb('" + brandName + "','" + id + "')>" + '合并' + "</button>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+
							"<button class='btn btn-primary'onclick=vm.del('" + id + "')>" + '删除' + "</button>";
					}
					return content;
				}
			},
        ],
		viewrecords: true,
		height: "100%",
		rowNum: 12,
		rowList : [12,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{'brandName':vm.q.brandName},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});
setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			// console.log(r);
			if(r.code==0){
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
			}
		}
	});
},2000);
function isNumber(val) {
	var regPos = /^\d+(\.\d+)?$/; //非负浮点数
	var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
	if(regPos.test(val) || regNeg.test(val)) {
		return true;
	} else {
		return false;
	}
}
function update(e) {
	vm.show=false;
	vm.showList = false;
	vm.title = "修改";

	vm.getInfo(e);
}
function add(e,x) {
	vm.showLists=false;
	vm.showList = false;
	vm.showList = false;
	vm.bBrandInfo.brandName=e;
	vm.bBrandInfo.id=x;
	$("#code").val("");
	$("#type").html("");
	$("#type").nextAll("p").html("");
	$('#in1').html("");
	$.ajax({
		type: "POST",
		url: baseURL +  'sys/barticletypeinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				vm.barticletypeinfo=r.barticletypeinfo;
				for(i=0;i<vm.barticletypeinfo.length;i++){
					$("#type").append(
						"<option  value=" + vm.barticletypeinfo[i].articleTypeName + "/>")
					;
					$(".list").append("<p hidden='hidden'  id=" + vm.barticletypeinfo[i].articleTypeName + ">"+vm.barticletypeinfo[i].id+"</p>")
					;
				}
			}
		}
	});
}
function hb(e,x) {
	vm.userId=x;
	layer.open({
		type: 1,
		title: '合并',
		maxmin: true,
		success: function(layero){
			layero.find('.layui-layer-btn').css('text-align', 'center')
		},
		shadeClose: true, //点击遮罩关闭层
		area : ['400px' , '180px'],
		content: $("#hebing").html()
	});
}
function del(e,x) {
	vm.bBrandInfo={};
	vm.bBrandInfo.id=e;
	vm.bBrandInfo.brandOpenStatus=x;
	confirm('确定修改品牌状态？', function(){
		var url ="sys/bbrandinfo/update";
		$.ajax({
			type: "POST",
			url: baseURL + url,
			contentType: "application/json",
			data: JSON.stringify(vm.bBrandInfo),
			success: function(r){
				if(r.code === 0){
					alert('操作成功', function(index){
						vm.back();
					});
				}else{
					alert(r.msg);
				}
			}
		});
	});
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			brandName:null
		},
		showList: true,
		show: true,
		hebing:true,
		showLists:true,
		title: null,
		bBrandInfo: {},
		userId:""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.show=false;
			vm.title = "新增";
			vm.bBrandInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.show=false;
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url ="sys/bbrandinfo/update";
			if(vm.bBrandInfo.brandName==null||vm.bBrandInfo.brandName==""){
				alert("请填写完整信息");
				return;
			}
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.bBrandInfo),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.back();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		Updates:function(){
			var length=$("#code").val();
			if(length<=0){
				// alert("请选择文章类型分类");
				// return;
				length=0;
			}else{
				length=length.substr(0,length.length-1);
			}
			vm.bBrandInfo.articleTypeIds=length;
			vm.bBrandInfo.handle_Status=1;
			if(vm.bBrandInfo.brandName==null||vm.bBrandInfo.brandName==""){
				alert("请填写完整信息");
				return;
			}
			// if(vm.bBrandInfo.articleTypeIds==""||vm.bBrandInfo.articleTypeIds==null){
			// 	alert("请选择分类");
			// 	return;
			// }
			$.ajax({
				type: "POST",
				url: baseURL + "sys/bbrandinfo/updates",
				contentType: "application/json",
				data: JSON.stringify(vm.bBrandInfo),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.back();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		batchMerge:function(){
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			layer.open({
				type: 1,
				title: '合并',
				maxmin: true,
				success: function(layero){
					layero.find('.layui-layer-btn').css('text-align', 'center')
				},
				shadeClose: true, //点击遮罩关闭层
				area : ['400px' , '180px'],
				content: $("#hebing").html()
			});
		},
		del: function (event) {
			var id = event;
			if(id == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/bbrandinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(id),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/bbrandinfo/info/"+id, function(r){
                vm.bBrandInfo = r.bBrandInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showLists = true;
			vm.show = true;
			$("#code").val("");
			$("#type").html("");
			$("#type").nextAll("p").html("");
			if(vm.q.brandName==null){
				vm.q.brandName="";
			}
			var page = 1;
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'brandName':vm.q.brandName,'gzhCode': vm.q.gzhCode},
                page:page
            }).trigger("reloadGrid");
		},
		back:function (event) {
			vm.showList = true;
			vm.showLists = true;
			vm.show = true;
			$("#code").val("");
			$("#type").html("");
			$("#type").nextAll("p").html("");
			if(vm.q.brandName==null){
				vm.q.brandName="";
			}
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'brandName':vm.q.brandName},
				page:page
			}).trigger("reloadGrid");
		}
	},
	created:function () {

	}
});